unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls,IniFiles,WinSvc, ImgList, RzButton, Mask, RzEdit,
  RzBtnEdt;

type
  TForm1 = class(TForm)
    Btnv_1: TButton;
    Btnv_2: TButton;
    Btnv_3: TButton;
    Btnv_4: TButton;
    Btnv_5: TButton;
    Btnv_6: TRzBitBtn;
    il1: TImageList;
    Btnv_7: TButton;
    Btnv_8: TButton;
    procedure Btnv_2Click(Sender: TObject);
    procedure Btnv_1Click(Sender: TObject);
    procedure Btnv_3Click(Sender: TObject);
    procedure Btnv_4Click(Sender: TObject);
    procedure Btnv_5Click(Sender: TObject);
    procedure Btnv_6Click(Sender: TObject);
    procedure Btnv_7Click(Sender: TObject);
    procedure Btnv_8Click(Sender: TObject);
  private
  public
    procedure FileFind(aPath:string;var Result:THashedStringList;untilFind:string='');
  end;

var
  Form1: TForm1;

implementation

uses
  Class_KzUtils,Class_KzDebug,Class_SvcUtil;

{$R *.dfm}

procedure TForm1.Btnv_2Click(Sender: TObject);
begin
  TKzUtils.Explore;
end;

procedure TForm1.FileFind(aPath: string;var Result:THashedStringList;untilFind:string);//lowercase
var
  F: TSearchRec;
begin
  if aPath[length(aPath)] <> '\' then
  begin
    aPath := aPath +'\';
  end;

  if FindFirst(aPath + '*.*', faDirectory  + faReadOnly + faHidden + faSysFile + faArchive, F) = 0 then
  begin
    repeat
      if F.Attr and faArchive > 0 then
      begin
        if (F.Name <> '.') and (F.name <> '..') then
        begin
          Result.Add(Format('%S=%S',[F.Name,aPath + F.Name]));
          if (untilFind<>'') and (LowerCase(untilFind)=LowerCase(F.Name)) then
          begin
            Break;
          end;
        end;
      end else
      if F.Attr and faDirectory > 0 then      
      begin
        if (F.Name <> '.') and (F.Name <> '..') then
        begin
          FileFind(aPath + F.Name,Result,untilFind);
        end;
      end;
    until FindNext(F) <> 0;
    FindClose(F);
  end;
end;

procedure TForm1.Btnv_1Click(Sender: TObject);
var
  cList:THashedStringList;
  cIndx:Integer;
  Value:string;
begin
  try
    cList:=THashedStringList.Create;
    FileFind('D:\GITOSC\HzWork',cList,'srvr.wlzf.zhjy.exe');
    ShowMessageFmt('finded.count:%D',[cList.Count]);
    KzDebug.FileFmt('%S:%S',[Self.ClassName,cList.Text]);
    cIndx:=cList.IndexOfName('SRVR.WLZF.ZHJY.exe');
    if cIndx<>-1 then
    begin
      Value:=cList.ValueFromIndex[cIndx];
      ShowMessageFmt('%S',[Value]);
      ShowMessageFmt('%S',[ExtractFilePath(Value)]);
      WinExec(PChar(Format('%s /uninstall',[Value])),SW_NORMAL);
    end;
  finally
    FreeAndNil(cList);
  end;
end;

procedure TForm1.Btnv_3Click(Sender: TObject);
var
  SCM,SVC:SC_HANDLE;
  Status : SERVICE_STATUS;
begin
  SCM:=OpenSCManager(nil,nil,SC_MANAGER_ALL_ACCESS);
  if SCM <> 0 then
  begin
    SVC := OpenService(SCM,PChar('srvr_wlzf_zhjy'),SERVICE_ALL_ACCESS);
    if SVC <> 0 then
    begin
      //#StartService(SVC,0,PChar(nil^));
      ControlService(SVC,SERVICE_CONTROL_STOP,Status);

      //#ControlService(SVC,SERVICE_CONTROL_CONTINUE,Status);
      if Status.dwCurrentState = SERVICE_STOP_PENDING then
      begin
        KzDebug.FileFmt('%S:%S',[self.ClassName,'SERVICE_STOP_PENDING']);      
      end else      
      if Status.dwCurrentState = SERVICE_STOPPED then
      begin
        KzDebug.FileFmt('%S:%S',[self.ClassName,'SERVICE_STOPPED']);
      end else
      if Status.dwCurrentState =  SERVICE_PAUSED then 
      begin
        KzDebug.FileFmt('%S:%S',[self.ClassName,'SERVICE_PAUSED']);      
      end else
      if Status.dwCurrentState = SERVICE_RUNNING then 
      begin
        KzDebug.FileFmt('%S:%S',[self.ClassName,'SERVICE_RUNNING']);
      end;

      CloseServiceHandle(SVC)
    end;

    CloseServiceHandle(SCM);
  end;
end;

procedure TForm1.Btnv_4Click(Sender: TObject);
var
  SCM,SVC:SC_HANDLE;
  Status : SERVICE_STATUS;
begin
  SCM:=OpenSCManager(nil,nil,SC_MANAGER_ALL_ACCESS);
  if SCM <> 0 then
  begin
    SVC := OpenService(SCM,PChar('SRVR_WLZF_ZHJY'),SERVICE_ALL_ACCESS);
    if SVC <> 0 then
    begin
      if QueryServiceStatus(SVC,Status) then
      begin
        if Status.dwCurrentState = SERVICE_STOPPED then
        begin
          KzDebug.FileFmt('%S:%S',[self.ClassName,'SERVICE_STOPPED']);
        end else
        if Status.dwCurrentState =  SERVICE_PAUSED then
        begin
          KzDebug.FileFmt('%S:%S',[self.ClassName,'SERVICE_PAUSED']);
        end else
        if Status.dwCurrentState = SERVICE_RUNNING then
        begin
          KzDebug.FileFmt('%S:%S',[self.ClassName,'SERVICE_RUNNING']);
        end;
      end;
      CloseServiceHandle(SVC);
    end else
    begin
      ShowMessageFmt('%S',['NO SUCH SERVICE']);
    end;
    CloseServiceHandle(SCM);
  end;
end;

procedure TForm1.Btnv_5Click(Sender: TObject);
begin
  TKzUtils.WarnMsg('HELLO');
  TKzUtils.WarnMsg('HELLO:%S',['WORLD']);

  TKzUtils.ErorMsg('HELLO');
  TKzUtils.ErorMsg('HELLO:%S',['WORLD']);  
end;

procedure TForm1.Btnv_6Click(Sender: TObject);
var
  Value:Integer;
begin
  //#Value:=StrComp('1.0.22.1','1.0.22.1');
  Value:=StrComp('9.9','10.8.8.6');  
  ShowMessageFmt('%D',[Value]);
end;

procedure TForm1.Btnv_7Click(Sender: TObject);
begin
  TSvcUtil.SvcEnded('SRVR_WLZF_ZHJY');
end;

procedure TForm1.Btnv_8Click(Sender: TObject);
begin
  TSvcUtil.SvcStart('SRVR_WLZF_ZHJY');
end;

end.
